#!/usr/bin/env python
# -*- coding:UTF-8 -*-
import sys
import traceback
import logging
import cgi,cgitb
from os import environ
from string import strip,split
import datetime,time
import MySQLdb
import base64
import socket
from struct import *
import ctypes
import json
import hashlib
import os

import MySQLDB

from django.conf import settings as original_settings

#import numpy as np

#功能：MYSQL数据库查询命令
#输出参数：HOST为IP地址,Name用户名,passwd密码,DBName为数据库名,Str为查询命令
#返回值：row
def PowerID(IPid):
    try:
        date = [0,0,0,0,0,0,0]
        row = MySQLDB.SelectMySql("SELECT menu_id FROM t_role_menu WHERE role_id = %s" % IPid)
        if len(row) > 0:
            for i in range(len(row)):
                tmp = int(row[i][0]) - 1
                date[tmp] = 1
        else:
            pass
        #while (IPid > 0):
        #    date.append(IPid%2)
        #    IPid = IPid/2
        #if len(date) < 7:
        #    for i in range(7 - len(date)):
        #        date.append(0)
        return date
    except Exception as e:
        return date

def roleid(request):
    menustr = []
    role_id = 0
    try:
        try:
            userid = request.GET['userid']
        except:
            userid = request.COOKIES['userid']
        #获取roleid
        row0 = MySQLDB.SelectMySql("SELECT role_id FROM t_sys_user WHERE user_id = %s;" % userid)
        try:
            role_id = row0[0][0]
            row1 = MySQLDB.SelectMySql("SELECT menu_id FROM t_role_menu WHERE role_id = %s;" % row0[0][0])
            menudate = []
            for i in range(len(row1)):
                menudate.append(row1[i][0])
        except:
            pass
        #获取菜单列表
        if 8 not in menudate:
            menustr.append(0)
        else:
            menustr.append(1)
        if 17 not in menudate:
            menustr.append(0)
        else:
            menustr.append(1)
        if 18 not in menudate:
            menustr.append(0)
        else:
            menustr.append(1)
        if 19 not in menudate:
            menustr.append(0)
        else:
            menustr.append(1)
        if 20 not in menudate:
            menustr.append(0)
        else:
            menustr.append(1)
        if 21 not in menudate:
            menustr.append(0)
        else:
            menustr.append(1)
        if 22 not in menudate:
            menustr.append(0)
        else:
            menustr.append(1)
        if 23 not in menudate:
            menustr.append(0)
        else:
            menustr.append(1)
        if 24 not in menudate:
            menustr.append(0)
        else:
            menustr.append(1)

        #日常巡查任务权限
        if 1 not in menudate and 2 not in menudate and  3 not in menudate:
            menustr.append(0)
        else:
            menustr.append(1)
    except:
        menustr = [0,0,0,0,0,0,0,0,0,0]
    try:
        row2 = MySQLDB.SelectMySql("SELECT real_name FROM t_sys_user WHERE user_id = %s;" % userid)
        user_name = row2[0][0]
    except:
        user_name = "null"
    return {'menustr': menustr, 'user_name':user_name}

